package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityImportedBreedingPigeonBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterImportedBreedingPigeon
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.finish
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/9/18 13:41
 * @Describe    : 导入种鸽列表
 */
class ActivityImportedBreedingPigeon : BaseActivity<BreedingPigeonViewModel, ActivityImportedBreedingPigeonBinding>() {

    private val mAdapter: AdapterImportedBreedingPigeon by lazy { AdapterImportedBreedingPigeon() }
    private lateinit var loadSir: LoadService<Any>
    private var mTitle = ""
    private var mPigeonSex = ""

    private var mLevel = 0
    private var mLevelInfo = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mTitle = intent.getStringExtra("title").toString()
        mDatabind.includeBar.tvTitle.text = mTitle

        mPigeonSex = if (mTitle.contains("母")) {
            getString(R.string.hint_female)
        } else if (mTitle.contains("父")) {
            getString(R.string.hint_male)
        } else {
            ""
        }

        refreshLevelInfo(mTitle)

        loadSir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            onRefresh(isRefresh = true)
        }

        onRefresh(isRefresh = true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 8f)))
            it.initFooter {
                onRefresh(isRefresh = false)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(isRefresh = true)
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(isRefresh = true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }

        mAdapter.addChildClickViewIds(R.id.btnImported)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.btnImported -> onRefreshDetail(mAdapter.data[position].id)
            }
        }
    }

    private fun refreshLevelInfo(mTitle: String) {
        when (mTitle) {
            "主鸽" -> {
                mLevel = 1
                mLevelInfo = "1"
            }

            "父亲" -> {
                mLevel = 2
                mLevelInfo = "1-1"
            }

            "母亲" -> {
                mLevel = 2
                mLevelInfo = "1-2"
            }

            "祖父" -> {
                mLevel = 3
                mLevelInfo = "1-1-1"
            }

            "祖母" -> {
                mLevel = 3
                mLevelInfo = "1-1-2"
            }

            "外祖父" -> {
                mLevel = 3
                mLevelInfo = "1-2-1"
            }

            "外祖母" -> {
                mLevel = 3
                mLevelInfo = "1-2-2"
            }

            "曾祖父" -> {
                mLevel = 4
                mLevelInfo = "1-1-1-1"
            }

            "曾祖母" -> {
                mLevel = 4
                mLevelInfo = "1-1-1-2"
            }

            "曾外祖父" -> {
                mLevel = 4
                mLevelInfo = "1-1-2-1"
            }

            "曾外祖母" -> {
                mLevel = 4
                mLevelInfo = "1-1-2-2"
            }

            "外曾祖父" -> {
                mLevel = 4
                mLevelInfo = "1-2-1-1"
            }

            "外曾祖母" -> {
                mLevel = 4
                mLevelInfo = "1-2-1-2"
            }

            "外曾外祖父" -> {
                mLevel = 4
                mLevelInfo = "1-2-2-1"
            }

            "外曾外祖母" -> {
                mLevel = 4
                mLevelInfo = "1-2-2-2"
            }

            "高祖父" -> {
                mLevel = 5
                mLevelInfo = "1-1-1-1-1"
            }

            "高祖母" -> {
                mLevel = 5
                mLevelInfo = "1-1-1-1-2"
            }

            "高外祖父" -> {
                mLevel = 5
                mLevelInfo = "1-1-1-2-1"
            }

            "高外祖母" -> {
                mLevel = 5
                mLevelInfo = "1-1-1-2-2"
            }

            "曾外曾祖父" -> {
                mLevel = 5
                mLevelInfo = "1-1-2-1-1"
            }

            "曾外曾祖母" -> {
                mLevel = 5
                mLevelInfo = "1-1-2-1-2"
            }

            "曾外曾外祖父" -> {
                mLevel = 5
                mLevelInfo = "1-1-2-2-1"
            }

            "曾外曾外祖母" -> {
                mLevel = 5
                mLevelInfo = "1-1-2-2-2"
            }

            "外高祖父" -> {
                mLevel = 5
                mLevelInfo = "1-2-1-1-1"
            }

            "外高祖母" -> {
                mLevel = 5
                mLevelInfo = "1-2-1-1-2"
            }

            "外高外祖父" -> {
                mLevel = 5
                mLevelInfo = "1-2-1-2-1"
            }

            "外高外祖母" -> {
                mLevel = 5
                mLevelInfo = "1-2-1-2-2"
            }

            "外曾外曾祖父" -> {
                mLevel = 5
                mLevelInfo = "1-2-2-1-1"
            }

            "外曾外曾祖母" -> {
                mLevel = 5
                mLevelInfo = "1-2-2-1-2"
            }

            "外曾外曾外祖父" -> {
                mLevel = 5
                mLevelInfo = "1-2-2-2-1"
            }

            "外曾外曾外祖母" -> {
                mLevel = 5
                mLevelInfo = "1-2-2-2-2"
            }
        }
    }

    private fun onRefreshDetail(mPigeonId: Int) {
        mViewModel.getBreedingPigeonInfo(mPigeonId.toString())
    }

    private fun onRefresh(isRefresh: Boolean) {
        val ringOrName = mDatabind.includeSearch.editQuery.text.toString().trim()
        if (isRefresh) {
            loadSir.showLoading()
        }
        mViewModel.getBreedingPigeonList(isRefresh, "", "", mPigeonSex, "", ringOrName, "", "", "")
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取种鸽详情 */
        mViewModel.detailResult.observe(this) {
            val intent = Intent()
            val bean = EditBloodInfoBean(
                publicIs = 1,
                country = it.country,
                countryEn = it.countryEn,
                eye = it.eye,
                featherColor = it.featherColor,
                gradesDesc = it.gradesDesc,
                name = it.name,
                ringId = it.year + "-" + it.area + "-" + it.ringId,
                sex = it.sex,
                title = mTitle,
                level = mLevel,
                levelInfo = mLevelInfo
            )
            intent.putExtra("data", bean)
            intent.putExtra("edit", 1)
            finish(intent)
        }
        /** 获取种鸽列表 */
        mViewModel.getPigeonResult.observe(this) {
            loadListData(it, mAdapter, loadSir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack, mDatabind.includeSearch.tvSearch, mDatabind.includeSearch.ivClose,
        ) {
            when (it) {
                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    onRefresh(isRefresh = true)
                }

                mDatabind.includeSearch.tvSearch -> {
                    if (mDatabind.includeSearch.editQuery.text.toString().trim().isEmpty()) {
                        RxToast.warning("请输入搜索内容")
                        return@setOnclickNoRepeat
                    }
                    onRefresh(isRefresh = true)
                }

                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}